package com.nycreativity.elecreativity.controller;


import com.nycreativity.elecreativity.aop.ValidateAspect
import com.nycreativity.elecreativity.entity.ResultData
import com.nycreativity.elecreativity.entity.cons.CommonURLConstants
import com.nycreativity.elecreativity.entity.dto.GoodsSpecificationDTO
import com.nycreativity.elecreativity.entity.param.PageParam
import com.nycreativity.elecreativity.entity.param.goods.GoodsAddParam
import com.nycreativity.elecreativity.entity.param.goods.GoodsQueryParam
import com.nycreativity.elecreativity.entity.param.goods.GoodsSpecificationUpdateParam
import com.nycreativity.elecreativity.entity.param.goods.GoodsUpdateParam
import com.nycreativity.elecreativity.entity.vo.PageDataVO
import com.nycreativity.elecreativity.entity.vo.goods.GoodsVO
import com.nycreativity.elecreativity.service.GoodsService
import com.nycreativity.elecreativity.util.PageUtil
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.validation.BindingResult
import org.springframework.web.bind.annotation.*

import javax.annotation.Resource
import javax.validation.Valid

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-03-22
 */
@RestController
@RequestMapping(CommonURLConstants.GOODS_PREFIX)
@Api(tags = ["商品信息相关接口"])
class GoodsController {

    @Resource
    lateinit var goodsService: GoodsService

    @GetMapping
    @ApiOperation("根据id查询")
    fun getById(id: Long): ResultData<GoodsVO> {
        val goodsVO = goodsService.queryById(id)
        return ResultData.SUCCESS(data = goodsVO)
    }

    @GetMapping("/all")
    @ApiOperation("查询全部")
    @Deprecated(message = "使用/page替代")
    fun getAll(): ResultData<List<GoodsVO>> {
        val list = goodsService.queryAll()
        return ResultData.SUCCESS(data = list)
    }

    @GetMapping("/page")
    @ApiOperation("按条件分页查找，都不传查全部")
    fun getPage(goodsQueryParam: GoodsQueryParam): ResultData<PageDataVO<GoodsVO>> {
        return ResultData.SUCCESS(data = PageUtil.convert(goodsService.queryPage(goodsQueryParam)))
    }

    @PostMapping
    @ApiOperation("新增")
    @ValidateAspect.ParamValidate
    fun add(@RequestBody @Valid goodsAddParam: GoodsAddParam, bindingResult: BindingResult): ResultData<Any> {
        val res = goodsService.add(goodsAddParam)
        return if (res) {
            ResultData.SUCCESS()
        } else {
            ResultData.FAILED("添加失败，请检查格式")
        }
    }

    @PutMapping
    @ApiOperation("根据id修改")
    @ValidateAspect.ParamValidate
    fun updateById(
        @RequestBody @Valid goodsUpdateParam: GoodsUpdateParam,
        bindingResult: BindingResult
    ): ResultData<Any> {
        val res = goodsService.updateById(goodsUpdateParam)
        return if (res) {
            ResultData.SUCCESS()
        } else {
            ResultData.FAILED("更新失败，请检查格式")
        }
    }

    @PutMapping("/specification")
    @ApiOperation("根据id修改规格")
    @ValidateAspect.ParamValidate
    fun updateSizeById(
        @RequestBody @Valid goodsSpecificationUpdateParam: GoodsSpecificationUpdateParam,
        bindingResult: BindingResult
    ): ResultData<Any> {
        goodsService.updateSpecificationById(goodsSpecificationUpdateParam)
        return ResultData()
    }

    @DeleteMapping
    @ApiOperation("根据id删除")
    fun deleteById(id: Long): ResultData<Any> {
        val res = goodsService.deleteById(id)
        return if (res) {
            ResultData.SUCCESS()
        } else {
            ResultData.FAILED("删除失败，请先删除该商品所有sku")
        }
    }

}

